local pojun = fk.CreateSkill {

  name = "joyex__pojun",

  tags = {  },

}



pojun:addEffect(fk.TargetSpecified, {
  name = "joyex__pojun",
  anim_type = "offensive",
  events = {fk.TargetSpecified,fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(pojun.name) and data.card and data.card.trueName == "slash" then
      if event == fk.TargetSpecified and player.phase == Player.Play then
        local to = player.room:getPlayerById(data.to)
        return to.hp > 0 and not to:isNude()
      elseif event == fk.DamageCaused then
        return not data.chain and #player:getCardIds(Player.Hand) >= #data.to:getCardIds(Player.Hand) and
        #player:getCardIds(Player.Equip) >= #data.to:getCardIds(Player.Equip)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TargetSpecified then
      local x = player.room:getPlayerById(data.to).hp + 1
      return player.room:askForSkillInvoke(player, pojun.name, nil, "#joyex__pojun-invoke::"..data.to..":"..x)
    elseif event == fk.DamageCaused then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local to = room:getPlayerById(data.to)
      local cards = room:askForCardsChosen(player, to, 1, to.hp + 1, "he", pojun.name)
      to:addToPile(pojun.name, cards, false, pojun.name)
      local equipC = table.filter(cards, function (id)
        return Fk:getCardById(id).type == Card.TypeEquip
      end)
      if #equipC > 0 then
        local card = room:askForCardChosen(player, to, { card_data = { { "$Throw", equipC }  } }, pojun.name, "#ty_ex__pojun-throw")
        room:throwCard({card}, pojun.name, to, player)
      end
      if not player.dead and table.find(cards, function (id)
        return Fk:getCardById(id).name == "jink"
      end) then
        player:drawCards(1, pojun.name)
      end
    elseif event == fk.DamageCaused then
      data.damage = data.damage + 1
    end
  end,
})
pojun:addEffect(fk.DamageCaused, {
  name = "joyex__pojun",
  anim_type = "offensive",
  events = {fk.TargetSpecified,fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(pojun.name) and data.card and data.card.trueName == "slash" then
      if event == fk.TargetSpecified and player.phase == Player.Play then
        local to = player.room:getPlayerById(data.to)
        return to.hp > 0 and not to:isNude()
      elseif event == fk.DamageCaused then
        return not data.chain and #player:getCardIds(Player.Hand) >= #data.to:getCardIds(Player.Hand) and
        #player:getCardIds(Player.Equip) >= #data.to:getCardIds(Player.Equip)
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TargetSpecified then
      local x = player.room:getPlayerById(data.to).hp + 1
      return player.room:askForSkillInvoke(player, pojun.name, nil, "#joyex__pojun-invoke::"..data.to..":"..x)
    elseif event == fk.DamageCaused then
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local to = room:getPlayerById(data.to)
      local cards = room:askForCardsChosen(player, to, 1, to.hp + 1, "he", pojun.name)
      to:addToPile(pojun.name, cards, false, pojun.name)
      local equipC = table.filter(cards, function (id)
        return Fk:getCardById(id).type == Card.TypeEquip
      end)
      if #equipC > 0 then
        local card = room:askForCardChosen(player, to, { card_data = { { "$Throw", equipC }  } }, pojun.name, "#ty_ex__pojun-throw")
        room:throwCard({card}, pojun.name, to, player)
      end
      if not player.dead and table.find(cards, function (id)
        return Fk:getCardById(id).name == "jink"
      end) then
        player:drawCards(1, pojun.name)
      end
    elseif event == fk.DamageCaused then
      data.damage = data.damage + 1
    end
  end,
})

pojun:addEffect(fk.TurnEnd, {
  name = "#joyex__pojun_delay",
  mute = true,
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return not player.dead and #player:getPile("joyex__pojun") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local dummy = Fk:cloneCard("zixing")
    dummy:addSubcards(player:getPile("joyex__pojun"))
    local room = player.room
    room:obtainCard(player, dummy, false)
  end,
})

return pojun